import java.util.LinkedList;
import java.util.Queue;

class MyStack {
    Queue<Integer> queue1;
    Queue<Integer> queue2;
     public int usedSize;

    public MyStack() {
        queue2=new LinkedList<>();
        queue1=new LinkedList<>();
    }
    
    public void push(int x) {
        if (!queue1.isEmpty()){
            queue1.offer(x);
        }else if (!queue2.isEmpty()){
            queue2.offer(x);
        }{
            queue1.offer(x);
        }
        usedSize++;
    }
    
    public int pop() {
        if (empty()){
            return -1;
        }
        if (!queue1.isEmpty()){
            int curSize=queue1.size();
            for (int i = 0; i < curSize; i++) {
                queue2.offer(queue1.poll());
            }
            usedSize--;
            return queue1.poll();
        }else {
            int curSize=queue2.size();
            for (int i = 0; i < curSize-1; i++) {
                queue1.offer(queue2.poll());
            }
            usedSize--;
            return queue2.poll();
        }
    }
    
    public int top() {
        if (empty()){
            return -1;
        }
        if (!queue1.isEmpty()){
            int curSize=queue1.size();
            int ret =-1;
            for (int i = 0; i < curSize; i++) {
                ret=queue1.poll();
                queue2.offer(ret);
            }
            return ret;
        }else {
            int curSize=queue2.size();
            int ret =-1;
            for (int i = 0; i < curSize; i++) {
                ret=queue2.poll();
                queue1.offer(ret);
            }
            return ret;
        }
    }
    
    public boolean empty() {
        return usedSize==0;
    }
}